﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Adex;
using MediaMonitor.Common;

namespace MediaMonitor.Admin
{
    public partial class Data_Mgmt_Sector : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = Adex.Sector.Query().SetSelectList("*, CONVERT(VARCHAR, DateModified, 101) AS LastModifiedDate ").DISTINCT().ExecuteDataSet().Tables[0];
            radGrid.DataSource = dt;
        }

        protected void radGrid_DeleteCommand(Object source, Telerik.Web.UI.GridCommandEventArgs e)
        {

            //check deletion of child rows
            if (e.Item.OwnerTableView.DataSourceID == "sqlDSCat")
            {
                string detailPkId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CategoryCode"].ToString();
                try
                {
                    Category.Delete(detailPkId);
                }
                catch
                {
                    AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
                }

                return;
            }



            //check deletion of child rows
            if (e.Item.OwnerTableView.DataSourceID == "sqlDSSubSec")
            {
                string detailPkId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["SubSectorCode"].ToString();
                try
                {
                    SubSector.Delete(detailPkId);
                }
                catch
                {
                    AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
                }

                return;
            }



            string pkId = radGrid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["SectorCode"].ToString();

            try
            {
                Sector.Delete(pkId);

            }
            catch
            {
                AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
            }

            radGrid.Rebind();
        }
    }
}
